package com.google.appengine.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.codelab.BaseServlet;
import com.google.appengine.codelab.Common;
import com.google.appengine.codelab.Fraude;
import com.google.appengine.codelab.Localidade;
import com.google.appengine.codelab.Transacao;

public class ClassesEquivalenciaServlet extends BaseServlet 
{
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		
		request.setAttribute("paises", Localidade.listarPaises());
		request.setAttribute("tipos", Fraude.listarTiposFraudes());
		request.setAttribute("tiposTransacao", Transacao.listarTiposTransacao());
		request.getRequestDispatcher("/WEB-INF/jsp/frmClassesEquivalencia.jsp").forward(request, response);		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)	throws ServletException, IOException 
	{
		String datainicial = request.getParameter("dataInicial");
		String dataFinal = request.getParameter("dataFinal");
		String pais = request.getParameter("pais");
		String estado = request.getParameter("estado");
		String cidade = request.getParameter("cidade");
		String tipoTransacao = request.getParameter("tipoTransacao");
		String tipo = request.getParameter("tipo");
		
		request.setAttribute("datainicial", datainicial);
		request.setAttribute("dataFinal", dataFinal);
		request.setAttribute("usuario", "Administrador");
		request.setAttribute("estado", estado);
		request.setAttribute("cidade", cidade);
		request.setAttribute("tipoTransacao", tipoTransacao);
		request.setAttribute("tipo", tipo);
		
		try{
			Date data1 = Common.cDate(datainicial);
			Date data2 = Common.cDate(dataFinal);
			
			if( data1.after( data2 ) ){
				throw new RuntimeException("Data final maior que data inicial.");
			}
			
			request.setAttribute("pais", pais);
			request.setAttribute("dataAtual", new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new Date()));
			
			request.getRequestDispatcher("/WEB-INF/jsp/relatorioClassesEquivalencia.jsp").forward(request, response);  
		}
		catch(Exception e){
			request.setAttribute("erro", e.getMessage());
			request.getRequestDispatcher("/WEB-INF/jsp/frmClassesEquivalencia.jsp").forward(request, response);
		}		
	}
}
